package fr.xebia.monitoring.servlet.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

import fr.xebia.monitoring.Monitor;
import fr.xebia.monitoring.MonitorFactory;

public class MonitoredServletFilter implements Filter {

	private static final String MONITORING_KEY_START = "servlet.[";
	private static final String MONITORING_KEY_END = "]";

	public void init(FilterConfig filterConfig) throws ServletException {
	}

	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,
			ServletException {
		Monitor monitor = MonitorFactory.start(MONITORING_KEY_START, ((HttpServletRequest)request).getRequestURI(), MONITORING_KEY_END);
		chain.doFilter(request, response);
		monitor.stop();
	}

	public void destroy() {
	}
}
